package com.example.citypolitics.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.citypolitics.Tools.RecordTool;
import com.example.citypolitics.Tools.ToolDateTime;
import com.example.citypolitics.model.GatewayResult;
import com.example.citypolitics.util.GatewayRequestUtil;

import com.jfinal.plugin.activerecord.Record;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class formService {


    @ApiOperation("新增表单实例")
    public String formSaveFormData() {

        GatewayResult result = null;
        try {

            Map<String, String> param = new HashMap<String, String>();
            param.put("appType", "APP_FM2YHMX1YFT75OIFHPWB");
            param.put("systemToken", "KP966YC1Q5PO0M1PXH3R47LDH1DX1RIQWGVMKEB");
            param.put("userId", "1022414637690092");
            param.put("formUuid", "FORM-FC8660919VPORW460WG094Q8TC0E33HUHMVMKNA");


            Date d = new Date();
         //   System.out.println(d);
            //     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateNowStr = sdf.format(d);
            System.out.println("格式化后的日期：" + dateNowStr);

            String res;

            Date date = sdf.parse(dateNowStr);
            long ts = date.getTime();
            res = String.valueOf(ts);

       //     System.out.println("将时间转换为时间戳：" + res);


           List listtemp= getGormSearchFormDatas();
           for (Object name : listtemp){
               Map<String, Object> temp = new HashMap<>();
               temp.put("textField_kmvmi0cz", name.toString());
               temp.put("textField_knjnm38e", dateNowStr);

               temp.put("textField_knjnm38d", res);
               temp.put("radioField_kninilfk", "否");
               temp.put("radioField_kninilfn", "否");
               temp.put("radioField_kninilfo", "否");

               String jsonData = JSON.toJSONString(temp);
               param.put("formDataJson", jsonData);

            //   log.info("新增表单实例"+ ToolDateTime.getDate());
               result = GatewayRequestUtil.baseRequest(param, "/yida_vpc/form/saveFormData.json");
           }



            //   System.out.println(result);


            return JSON.toJSONString(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    @ApiOperation("获取项目列表")
    public List getGormSearchFormDatas() {

        List listTemp = new ArrayList();
        GatewayResult result = null;
        try {

            Map<String, String> param = new HashMap<String, String>();
            param.put("appType", "APP_FM2YHMX1YFT75OIFHPWB");
            param.put("systemToken", "KP966YC1Q5PO0M1PXH3R47LDH1DX1RIQWGVMKEB");
            param.put("userId", "1022414637690092");
            param.put("formUuid", "FORM-2O8661A1N5POPS9J41C6PCNEEBZN3Q8O8HVMK9F");


         //   log.info("根据条件搜索表单实例详情列表"+ ToolDateTime.getDate());
            result = GatewayRequestUtil.baseRequest(param, "/yida_vpc/form/searchFormDatas.json");

            //   System.out.println(result);
    //        System.out.println(JSON.toJSON(result.getResult()));
            JSONObject jb = JSON.parseObject(JSON.toJSONString(result));

            JSONArray array = jb.getJSONArray("data");
            List<Record> rlist = RecordTool.jsonToRecordList(result.getResult(), "data");
            for (Record r : rlist) {  //每个实例
/*
                log.error("实例id==================================" + r.get("formInstId") + "");
                log.error("创建时间==================================" + r.get("gmtModified") + "");
                log.error("表单id==================================" + r.get("formUuid") + "");
                log.error("用户列表==================================" + r.get("name") + "");
*/


                String processInstance = r.get("formInstId") + "";//实例

                String data = r.get("formData").toString();
                JSONObject baseData = JSON.parseObject(data);
                String imageInfo = baseData.getString("textField_kmvh8x4x");
                log.info("项目名称==================================" + imageInfo);
                listTemp.add(imageInfo);
            }

            return listTemp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listTemp;
    }
}
